<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of phpExcel
 *
 * @author MZ
 */
class phpExcel {
    //put your code here
    private $head;
    private $body;

    /**
     *
     * @param type $arr 一维数组
     */
    public function addHeader($arr){
        foreach($arr as $headVal){
            $headVal = $this->charset($headVal);
            $this->head .= "<th>{$headVal}</th> ";
        }
        $this->head = "<tr>".$this->head.'</tr>';
    }

    /**
     *
     * @param type $arr 二维数组
     */
    public function addBody($arr){
        foreach($arr as $arrBody){
            $tmp = '<tr>';
            foreach($arrBody as $bodyVal){
                $bodyVal = $this->charset($bodyVal);
                // 过滤特殊字符
                $bodyVal = str_replace(array('\\n','\\t','<br>','<br />', '<br>','</br>'), "", $bodyVal);
                $bodyVal = str_replace(array("rn", "r", "n"), "", $bodyVal);
                $bodyVal =preg_replace("{\t}","",$bodyVal);
                $bodyVal=preg_replace("{\r\n}","",$bodyVal);
                $bodyVal=preg_replace("{\r}","",$bodyVal);
                $bodyVal=preg_replace("{\n}","",$bodyVal);
                $bodyVal = str_replace(",", " ",$bodyVal);
                $bodyVal = str_replace(" '", "'",$bodyVal);
                if(strpos($bodyVal,':@')===FALSE){
                    $tmp .= "<td>{$bodyVal}</td> ";
                }else{
                    $tmp .= "<td style='vnd.ms-excel.numberformat:@'>".str_replace(":@", "",$bodyVal)."</td> ";
                }

            }
            $this->body .= $tmp."</tr>";
        }
    }

    /**
     * 下载excel文件
     */
    public function downLoad($filename=''){
        if(!$filename)
            $filename = date('YmdHis',time()).'.xls';
        ob_end_clean();//清除缓冲区,避免乱码
        header("Content-type:application/vnd.ms-excel;charset=utf-8");
        header("Content-Disposition:attachment;filename=$filename");
        header("Content-Type:charset=gb2312");
        echo <<<xls
<html xmlns:o="urn:schemas-microsoft-com:office:office"
   xmlns:x="urn:schemas-microsoft-com:office:excel"
   xmlns="http://www.w3.org/TR/REC-html40">
<head>
   <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
   <meta http-equiv=Content-Type content="text/html; charset=gb2312">
   <!--[if gte mso 9]><xml>
   <x:ExcelWorkbook>
   <x:ExcelWorksheets>
     <x:ExcelWorksheet>
     <x:Name></x:Name>
     <x:WorksheetOptions>
       <x:DisplayGridlines/>
     </x:WorksheetOptions>
     </x:ExcelWorksheet>
   </x:ExcelWorksheets>
   </x:ExcelWorkbook>
   </xml><![endif]-->
</head><table>
xls;
        if($this->head)
            echo $this->head;
        echo $this->body;
        echo '</table>';
    }

    /**
     * 编码转换
     * @param type $string
     * @return string
     */
    public function charset($string){
       // return iconv("utf-8", "gb2312//IGNORE", $string);
        //return iconv("utf-8", "gb2312//TRANSLIT", $string);
       // return iconv("utf-8", "gb2312", $string);
        $encode= mb_convert_encoding($string, 'GBK', 'UTF-8');
       return $encode;
    }
}
